package be.kuleuven.mume.paginas;

import java.util.ArrayList;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import be.kuleuven.mume.R;
import be.kuleuven.mume.framework.Mobilenvi;
import be.kuleuven.mume.framework.Vak;
import be.kuleuven.mume.hulpklassen.DataFetcher;
import be.kuleuven.mume.hulpklassen.JSONParser;

public class VakTabBar extends TabActivity{
	private Vak vak;
	private static final String VAK = "VAK";
	public static final String VAK_ID = "VAK_ID";
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.vaktab);
	    
	    tryLoadingData();
	}

	private void tryLoadingData() {
	    Bundle extras = getIntent().getExtras();
	    if(vak == null && extras != null) {
	    	vak = (Vak)extras.get(VAK);
	    	if(vak!=null){
		    	receivedVak();
		    	return;
	    	}
	    }
	    String vakId = (String)getIntent().getExtras().get(VAK_ID);
	    if(vakId != null){
	    	fetchData(vakId);
	    	return;
	    }
	    
	    vakId = getIntent().getData().getPath().substring(1);
	    fetchData(vakId);
	    
	    Log.e("VakTabBar", "No vakInfo found to load vak.");
	}

	private void fetchData(String vakId) {
		JSONParser<Vak> parser = new JSONParser.VakParser();
		final ArrayList<Vak> list = new ArrayList<Vak>();
		DataFetcher<Vak> fetcher = new DataFetcher<Vak>(parser, Vak.getVakUrl(vakId),null, list, this) {
			
			@Override
			public void DataReceived() {
				assert (list.size() == 0);
				vak = list.get(0);
				receivedVak();
			}
		};
		fetcher.fetchDataInNewThread();
	}

	private void receivedVak() {
        setTitle(vak.getName());
        
        addTab(VakInfoTab.class, "Info");
        addTab(VakGroepTab.class, "Groep");
        addTab(VakStudentenTab.class, "Medestudenten");
        addTab(VakUpdatesPagina.class, "Updates");

        TabHost tabHost = getTabHost();
        Uri uri = getIntent().getData();
        if( uri != null) {
        	if(uri.getHost().equals("TwitterSignin"))
        		tabHost.setCurrentTab(2);
        }
        else
        	tabHost.setCurrentTab(0);
	    
        int childCount = tabHost.getTabWidget().getChildCount();
	    for(int i=0; i<childCount; i++)
	    	tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = Mobilenvi.tabHeight;
	}

	private void addTab(Class<? extends Activity> pagina, String title ) {
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, pagina);
	    intent.putExtra("VAK", vak);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec(title).setIndicator(title)
	                  .setContent(intent);
	    tabHost.addTab(spec);
	}
}
